from urllib import request, parse

# urlretrieve 函数用法
# resp = request.urlretrieve("http://www.taobao.com","taobao.html")


# resp = request.urlopen("https://www.baidu.com/s?wd=育华志远")
# 请求参数
# data = {"wd":"育华志远"}
# 转码为16进制码编码
# paramData = parse.urlencode(data)
# print(paramData)
# resp = request.urlopen("http://www.baidu.com/s?wd=%E8%82%B2%E5%8D%8E%E5%BF%97%E8%BF%9C"+"?"+paramData)
# print(str(resp.read(), "utf-8"))
# # urlencode函数
# data = {"name":"张三","age":18,"info":"hello world"}
#
# encodeData = parse.urlencode(data)
#
# print(encodeData)
#
# # 案例 百度搜索育华志远
#
# result = parse.parse_qs(encodeData)
# print(result)


# if __name__ == '__main__':
#     data = {"keyword": "爬虫"}
#     # 转码为16进制码编码
#     paramData = parse.urlencode(data)
#     print(paramData)
#     # 解码为正常文字
#     resultData =parse.parse_qs(paramData)
#     print(resultData)

if __name__ == '__main__':
    # 封装请求参数
    data ={"keyword":"小米"}
    # 进行转码
    paramData = parse.urlencode(data)
    # 拼接URL
    url = "http://123.57.29.238:8378/search?"+paramData
    # 请求URL
    resp = request.urlopen(url)
    # 获取请求结果
    print(resp.read().decode())